package com.cn.studySpring.controller;

import com.cn.studySpring.pojo.User;
import com.cn.studySpring.service.UserService;
import com.cn.studySpring.util.WordDefined;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.util.List;

/**
 * @ClassName SpittleController
 * @Description TODO
 * @Author Administrator
 * @Date 2018/9/30 10:54
 * @Version 1.0
 */

@Controller
@RequestMapping("/Spittle")
public class SpittleController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String showRegisterForm(){
        return "/bootStrap/login";
    }


    /**
     * 登陆系统
     */
    @Value("${save.pic}")

    private String path;
    @RequestMapping(value = "/goToSystem",method = RequestMethod.POST)
    public String goToSystem(String userName, String passWord, MultipartFile file,RedirectAttributes attributes)throws Exception{
        if("".equals(file.getName())){
            //可以直接写入本地磁盘
            file.transferTo(new File(path,file.getOriginalFilename()));
        }
        User user = new User();
        user.setUserid(userName);
        user.setPassword(passWord);
        user.setStatus(1);
        int flag =userService.addUser(user);
        if(flag==1){
            attributes.addFlashAttribute("message", WordDefined.REGIST_DETAIL_USER_LOGIN);
            return "redirect:/user/login";
        }else{
            attributes.addFlashAttribute("error", "注册失败");
            return "redirect:Spittle/goToSystem";
        }
    }

    @RequestMapping(value = "/getAllUser",method = RequestMethod.GET)
    public String getAllUser(Model model){
   /*     map.put("names", Arrays.asList("caoyc","zhh","cjx"));
        model.addAttribute("time", new Date());
        modelMap.addAttribute("city", "ChengDu");
        modelMap.put("gender", "male");*/
        List<User> accountList =userService.getAllUser();
        model.addAttribute("accountList", accountList);
        return "/bootStrap/tables";
    }

    @RequestMapping(value = "/redirct",method = RequestMethod.GET)
    public String redirectAttribute(RedirectAttributes model){

        model.addAttribute("name","hello");
        model.addFlashAttribute("account", new User());//要传递对象就要用FlashAttribute
        return "redirect:/{name}";
    }

    @RequestMapping(value = "/chart",method = RequestMethod.GET)
    public String goChart(RedirectAttributes model){
        return "chart/chartroom";
    }
}
